package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.CoolTowerProjectDetail;
import com.hongpu.dto.CoolTowerDto;
import com.hongpu.service.CoolTowerProjectDetailService;
import com.hongpu.mapper.CoolTowerProjectDetailMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Michael zhang
* @description 针对表【cool_tower_project_detail(安装项目的冷却塔详情)】的数据库操作Service实现
* @createDate 2025-06-10 15:50:54
*/
@Service
public class CoolTowerProjectDetailServiceImpl extends ServiceImpl<CoolTowerProjectDetailMapper, CoolTowerProjectDetail>
    implements CoolTowerProjectDetailService{

    @Autowired
    CoolTowerProjectDetailMapper baseMapper;
    /**
     * 保存
     *
     * @param coolTowerProjectDetail
     */
    @Override
    public boolean toSave(CoolTowerProjectDetail coolTowerProjectDetail) {
        try  {
            boolean save = save(coolTowerProjectDetail);
            return save;
          } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据 coolTowerProjectId 删除 cool_tower_project_detail中数据
     *
     * @param coolTowerProjectId
     */
    @Override
    public boolean deleteByCoolTowerProjectId(Long coolTowerProjectId) {
       try {
           return baseMapper.deleteByCoolTowerProjectId(coolTowerProjectId);
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
    }

    /**
     * 删除项目时，将cool_tower_project_detail中数据状态设置为删除状态
     *
     * @param coolTowerProjectId
     */
    @Override
    public boolean updateDeleted(Long coolTowerProjectId) {
        return baseMapper.updateDeleted(coolTowerProjectId,  SecurityContextUtil.getUserId());
    }

    /**
     * 激活项目时，将cool_tower_project_detail中数据状态设置为激活状态
     *
     * @param coolTowerProjectId
     */
    @Override
    public boolean updateActive(Long coolTowerProjectId) {
        return baseMapper.updateActive(coolTowerProjectId,  SecurityContextUtil.getUserId());
    }

    /**
     * 根据coolTowerProjectId查询冷却塔信息，信息数据格式：型号（数量）
     *
     * @param coolTowerProjectId
     */
    @Override
    public String selectCoolTowerInfoByCoolTowerProjectId(Long coolTowerProjectId) {
       try  {
           String result = "";
           List<String> coolTowerInfos = baseMapper.selectCoolTowerInfoByCoolTowerProjectId(coolTowerProjectId);
           if(CollectionUtils.isNotEmpty(coolTowerInfos)){
               StringBuilder  stringBuilder = new StringBuilder();
               for (int i = 0; i < coolTowerInfos.size(); i++) {
                    stringBuilder.append(coolTowerInfos.get(i)).append(",");
                   if (i == coolTowerInfos.size()-1){
                       stringBuilder.deleteCharAt(stringBuilder.length()-1);
                   }
               }
               result = stringBuilder.toString();
           }
           return result;
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
    }

    /**
     * 根据coolTowerProjectId查询cool_tower_project_detail中数据
     *
     * @param coolTowerProjectId
     */
    @Override
    public List<CoolTowerDto> selectByCoolTowerProjectId(Long coolTowerProjectId) {
        try {
            List<CoolTowerDto> coolTowerDtos = new ArrayList<>();
            QueryWrapper<CoolTowerProjectDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cool_tower_project_id", coolTowerProjectId);
            List<CoolTowerProjectDetail> coolTowerProjectDetails = baseMapper.selectList(queryWrapper);
            if(CollectionUtils.isNotEmpty(coolTowerProjectDetails)){
                for (CoolTowerProjectDetail coolTowerProjectDetail : coolTowerProjectDetails) {
                    CoolTowerDto coolTowerDto = new CoolTowerDto();
                    coolTowerDto.setModel(coolTowerProjectDetail.getCoolTowerInfoModel());
                    coolTowerDto.setNumber(coolTowerProjectDetail.getNum());
                    coolTowerDtos.add(coolTowerDto);
                }
            }
            return coolTowerDtos;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




